// pages/address/address.js

import {
  getbaseUrl,
  requestUtil
} from "../../utils/request"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab: 2,
    screenHeight: 0,
    isShow: true,
    showPopup: false,
    shopAddress: [],
    markerArr: [],
    shopInfo: {},
    mark: [],
    red: 0,

    dialogShow: false
  },
  // 开弹窗
  getAddressInfo(e) {
    let id = e.currentTarget.dataset.id
    let status = e.currentTarget.dataset.status
    if (status == 0) {
      this.setData({
        dialogShow:true
      })
    } else {
      let shopInfo = this.data.shopAddress[id]

      let mark = [{
        id: shopInfo.id, //标记点 id
        width: 50,
        longitude: shopInfo.longitude,
        latitude: shopInfo.latitude,
        height: 30,
        title: shopInfo.shopAddress
      }]

      this.setData({
        showPopup: true,
        shopInfo: shopInfo,
        mark: mark
      });
    }


  },

  //跳转到商品列表
  toOrderList(e) {
    // console.log(e.detail)
    let {
      distance,
      shopname,
      id,
      storeaddress,
      storeimg
    } = e.detail
    // console.log(e.detail,"fesfse")

    wx.navigateTo({
      url: `/pages/order/order?shopname=${shopname}&distance=${distance}&storeId=${id}&storeaddress=${storeaddress}&storeImg=${storeimg}`,
    })
  },

  // 展示地图信息
  showMap(e) {
    // console.log(e.currentTarget.dataset)
    let {
      latitude,
      longitude
    } = e.currentTarget.dataset
    wx.openLocation({
      latitude: parseFloat(latitude),
      longitude: parseFloat(longitude),
      success: (res) => {
        // console.log(res)
      },
      fail: (err) => {
        // console.log(err)
      }
    })
  },

  // 拨打手机号
  dialNumber(e) {
    let {
      phone
    } = e.currentTarget.dataset

    wx.makePhoneCall({
      phoneNumber: phone,
      success: (res) => {
        console.log(res)
      },
      fail: (err) => {
        console.log(err)
      }
    })

  },

  //关弹窗
  // onClose() {
  //   this.setData({
  //     showPopup: false
  //   });
  // },

  // 屏幕高度
  screenHieght(e) {
    // console.log(e.detail)
    this.setData({
      isShow: e.detail
    })
  },

  // 获取店铺的地址信息
  getShopAddressInfo() {
    let res = requestUtil({
      url: "/shop/getShopAddress",
      methed: 'get'
    })
    res.then(({
      data
    }) => {
      // console.log(data.data)
      this.setData({
        shopAddress: data.data
      })
      this.setmark()
    })
  },

  // 地图标记点
  setmark() {
    let newMarker = []
    this.data.shopAddress.forEach(item => {
      newMarker.push({
        id: item.id, //标记点 id
        width: 50,
        longitude: item.longitude,
        latitude: item.latitude,
        height: 50,
        title: item.shopAddress,
        iconPath: getbaseUrl() + "/image/" + "yao.png"
      }, )
    })

    this.setData({
      markerArr: newMarker
    })

  },


  changeAct(e) {
    // console.log(e.currentTarget.dataset.index)
    let ind = e.currentTarget.dataset.index
    this.setData({
      activeTab: ind
    })
  },


  onChange(event) {
    wx.showToast({
      title: `切换到标签 ${event.detail.name}`,
      icon: 'none',
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  //获取系统的信息
  getsystInfo() {
    wx.getSystemInfo({
      success: (result) => {
        // console.log(result)
        if (result.model == "iPhone 5" || result.model == "iPhone SE (2nd generation)<iPhone12,8>") {
          this.setData({
            red: 0,
            screenHeight: (result.windowHeight - 250 - 18)
          })
        } else {
          this.setData({
            red: 100,
            screenHeight: (result.windowHeight + 20 - 250 - 48 - 40)
          })
        }

      },
    })
  },

  // 获取用户的位置


  onLoad: function (options) {
    //获取系统信息
    this.getsystInfo()

    //获取商品地址
    this.getShopAddressInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})